package com.example.hidesms;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hidesms.adapter.BlockCallAdapter;
import com.example.hidesms.adapter.BlockSmsAdapter;
import com.example.hidesms.bo.BlockCallBO;
import com.example.hidesms.bo.SmsBO;
import com.example.hidesms.dto.BlockCallItem;
import com.example.hidesms.dto.CallInfo;
import com.example.hidesms.dto.SMSItem;
import com.example.hidesms.receiver.SmsFilter;
import com.example.hidesms.sqlite.DataBaseHelper;
import com.example.hidesms.utils.Utils;

public class ListBlockInComing extends Fragment {
	private ListView				listView;
	private ArrayList<BlockCallItem> lstSMS;
	private BlockCallAdapter			mAdapter;
	private ProgressBar				mProgressBar;
	private SmsFilter				smsFilter;
	private String					address	= null;
	private TextView				tvAdd;

	public void setAddress(String address) {
		this.address = address;
		getData(address);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View view = inflater.inflate(R.layout.block_sms_screen, container, false);
		getElementFromUI(view);
		return view;
	}

	private void showDialog() {
		FragmentManager fm = getFragmentManager();
		AddBlockSmsNumberDialog ab = new AddBlockSmsNumberDialog();
		ab.show(fm, "add_dialog");
	}

	private void getElementFromUI(View view) {
		listView = (ListView) view.findViewById(R.id.list);
		tvAdd = (TextView) view.findViewById(R.id.tvAdd);
		tvAdd.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog();
			}
		});
		mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar);
		if (smsFilter == null) {
			smsFilter = new SmsFilter();
			getActivity().registerReceiver(smsFilter, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
		}
		getData(address);

	}

	private void getData(final String address) {
		mProgressBar.setVisibility(View.VISIBLE);
		// File dbFile = getActivity().getDatabasePath("PrivaceSms.db");
		// Log.e("getdata"," path "+dbFile.getAbsolutePath());
		Thread th = new Thread(new Runnable() {

			@Override
			public void run() {
				DataBaseHelper myDatabase = new DataBaseHelper(getActivity());
				lstSMS = BlockCallBO.getInstance().getAll(myDatabase, "1", false);
				 lstSMS = BlockCallBO.getInstance().getByAdress(myDatabase,
				 "MuACHUNG");
				// Collections.sort(lstSMS, new Comparator<SMSItem>(){
				// @Override
				// public int compare(SMSItem s1, SMSItem s2) {
				// return s1.getTimeOr().compareToIgnoreCase(s2.getTimeOr());
				// }
				//
				// });
//				try {
//					lstSMS = Utils.getCallsFromXML(getActivity());
//					Log.e("getdata", "det " +lstSMS.size());
//				} catch (XmlPullParserException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
				myDatabase.close();
				getActivity().runOnUiThread(new Runnable() {

					@Override
					public void run() {
						if (lstSMS.size() > 0) {
							Log.e("getdata", "mAdapter " + mAdapter);
							if (mAdapter == null) {
								mAdapter = new BlockCallAdapter(getActivity(), -1, lstSMS);
								listView.setAdapter(mAdapter);
							} else {
								mAdapter.setList(lstSMS);
								mAdapter.notifyDataSetChanged();
							}
							mProgressBar.setVisibility(View.GONE);
						} else {
							Toast.makeText(getActivity(), "ko co so dt", Toast.LENGTH_SHORT).show();
							mProgressBar.setVisibility(View.GONE);
						}
					}
				});

			}
		});
		th.start();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, 123, 1, "fuck");
		menu.add(1, 321, 3, "fuck");
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		SubMenu sub = menu.addSubMenu("Group");// .setIcon(R.drawable.tab_selected);
		sub.add(0, R.style.AppTheme, 0, "Date");
		sub.add(0, R.style.AppTheme, 0, "Phone");
		// sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS |
		// MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		SubMenu sub1 = menu.addSubMenu("Filter");// .setIcon(R.drawable.group_selected);
		sub1.add(0, R.style.AppTheme, 0, "Default");
		sub1.add(0, R.style.AppTheme, 0, "Light");
		// sub1.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS |
		// MenuItem.SHOW_AS_ACTION_WITH_TEXT);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (smsFilter != null) {
			getActivity().unregisterReceiver(smsFilter);
			smsFilter = null;
		}
	}

	public static ListBlockInComing instance() {
		ListBlockInComing fragment = new ListBlockInComing();
		Bundle bunle = new Bundle();
		fragment.setArguments(bunle);
		return fragment;

	}

}

class BlockCallRetain implements Parcelable {
	public BlockCallRetain() {

	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub

	}

}
